package cn.turboinfo.dongying.api.domain.admin.usecase.activity;

import cn.turboinfo.dongying.api.domain.common.service.activity.ActivityService;
import cn.turboinfo.dongying.api.domain.common.service.file.FileAttachmentService;
import cn.turboinfo.dongying.api.domain.common.usecase.AbstractUseCase;
import cn.turboinfo.dongying.api.domain.web.component.file.FileRefTypeConstant;
import cn.turboinfo.dongying.api.entity.common.pojo.activity.Activity;
import cn.turboinfo.dongying.api.entity.common.pojo.activity.QActivity;
import cn.turboinfo.dongying.api.entity.common.pojo.file.FileAttachment;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.sunshow.toolkit.core.qbean.api.request.QPage;
import net.sunshow.toolkit.core.qbean.api.request.QRequest;
import net.sunshow.toolkit.core.qbean.api.response.QResponse;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Component
public class ActivitySearchUseCase extends AbstractUseCase<ActivitySearchUseCase.InputData, ActivitySearchUseCase.OutputData> {
    private final ActivityService activityService;
    private final FileAttachmentService fileAttachmentService;

    @Override
    protected OutputData doAction(InputData inputData) {

        Long shopId = inputData.getShopId();
        QRequest request = inputData.getRequest();
        if (shopId != null) {
            request.filterEqual(QActivity.shopId, shopId);
        }

        QResponse<Activity> response = activityService.findAll(request, inputData.getRequestPage());

        Set<Long> activityIdSet = response.getPagedData().stream()
                .map(Activity::getId)
                .collect(Collectors.toSet());

        Map<Long, List<FileAttachment>> fileMap = fileAttachmentService.findByRefIdInAndRefType(activityIdSet, FileRefTypeConstant.ACTIVITY_IMG)
                .stream()
                .collect(Collectors.groupingBy(FileAttachment::getRefId));

        // 查询企业
        Set<Long> showIdSet = response.getPagedData().stream()
                .filter(it -> it.getShopId() != null && it.getShopId() > 0)
                .map(Activity::getShopId)
                .collect(Collectors.toSet());

        response.getPagedData().forEach(it -> {

            if (fileMap.get(it.getId()) == null) {
                return;
            }
            it.setImageIdList(
                    fileMap.get(it.getId())
                            .stream()
                            .map(FileAttachment::getId)
                            .collect(Collectors.toList())
            );
        });

        return OutputData.builder()
                .qResponse(response)
                .build();
    }

    @Getter
    @Setter
    @Builder
    public static class InputData extends AbstractUseCase.InputData {

        private Long shopId;

        private QRequest request;

        private QPage requestPage;
    }

    @Getter
    @Setter
    @Builder
    public static class OutputData extends AbstractUseCase.OutputData {
        private QResponse<Activity> qResponse;
    }
}
